package com.vst.test.generate.structure.springmvc.plugin.builder;

import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class EstructureClassBuilder extends IncrementalProjectBuilder {

	class SampleDeltaVisitor implements IResourceDeltaVisitor {
		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
		 */
		public boolean visit(IResourceDelta delta) throws CoreException {
			System.out.println("EstructureNatureClassBuilder SampleDeltaVisitor visit desc:"+delta);
			IResource resource = delta.getResource();
			System.out.println("EstructureNatureClassBuilder SampleDeltaVisitor resource:"+resource);
			System.out.println("EstructureNatureClassBuilder SampleDeltaVisitor delta.getKind():"+delta.getKind());
			switch (delta.getKind()) {
			case IResourceDelta.ADDED:
				System.out.println("EstructureNatureClassBuilder SampleDeltaVisitor IResourceDelta.ADDED");
				// handle removed ADDED
				checkXML(resource);
				break;
			case IResourceDelta.REMOVED:
				System.out.println("EstructureNatureClassBuilder SampleDeltaVisitor IResourceDelta.REMOVED");
				// handle removed resource
				break;
			case IResourceDelta.CHANGED:
				System.out.println("EstructureNatureClassBuilder SampleDeltaVisitor IResourceDelta.CHANGED");
				// handle changed resource
				checkXML(resource);
				break;
			}
			//return true to continue visiting children.
			return true;
		}
	}

	class SampleResourceVisitor implements IResourceVisitor {
		public boolean visit(IResource resource) {
			System.out.println("EstructureNatureClassBuilder SampleResourceVisitor resource:"+resource);
			checkXML(resource);
			//return true to continue visiting children.
			return true;
		}
	}

	class XMLErrorHandler extends DefaultHandler {
		
		private IFile file;

		public XMLErrorHandler(IFile file) {
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler file:"+file);
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler file:"+file.getName());
			this.file = file;
		}

		private void addMarker(SAXParseException e, int severity) {
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler addMarker e:"+e);
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler addMarker severity:"+severity);
			EstructureClassBuilder.this.addMarker(file, e.getMessage(), e
					.getLineNumber(), severity);
		}

		public void error(SAXParseException exception) throws SAXException {
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler error exception:"+exception);
			addMarker(exception, IMarker.SEVERITY_ERROR);
		}

		public void fatalError(SAXParseException exception) throws SAXException {
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler fatalError exception:"+exception);
			addMarker(exception, IMarker.SEVERITY_ERROR);
		}

		public void warning(SAXParseException exception) throws SAXException {
			System.out.println("EstructureNatureClassBuilder XMLErrorHandler warning exception:"+exception);
			addMarker(exception, IMarker.SEVERITY_WARNING);
		}
	}

	public static final String BUILDER_ID = "com.vst.test.generate.structure.springmvc.plugin.EstructureIDBuilder";

	private static final String MARKER_TYPE = "com.vst.test.generate.structure.springmvc.plugin.xmlProblem";

	private SAXParserFactory parserFactory;

	private void addMarker(IFile file, String message, int lineNumber,
			int severity) {
		System.out.println("EstructureNatureClassBuilder addMarker file:"+file);
		System.out.println("EstructureNatureClassBuilder addMarker message:"+message);
		System.out.println("EstructureNatureClassBuilder addMarker lineNumber:"+lineNumber);
		System.out.println("EstructureNatureClassBuilder addMarker severity:"+severity);
		try {
			IMarker marker = file.createMarker(MARKER_TYPE);
			System.out.println("EstructureNatureClassBuilder addMarker marker:"+marker);
			marker.setAttribute(IMarker.MESSAGE, message);
			marker.setAttribute(IMarker.SEVERITY, severity);
			System.out.println("EstructureNatureClassBuilder addMarker lineNumber:"+lineNumber);
			if (lineNumber == -1) {
				lineNumber = 1;
			}
			marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
		} catch (CoreException e) {
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.internal.events.InternalBuilder#build(int,
	 *      java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
			throws CoreException {
		System.out.println("EstructureNatureClassBuilder build kind:"+kind);
		System.out.println("EstructureNatureClassBuilder build args:"+args);
		System.out.println("EstructureNatureClassBuilder build monitor:"+monitor);
		if (kind == FULL_BUILD) {
//			fullBuild(monitor);
		} else {
			System.out.println("EstructureNatureClassBuilder build getProject():"+getProject());
			IResourceDelta delta = getDelta(getProject());
			System.out.println("EstructureNatureClassBuilder build delta:"+delta);
//			if (delta == null) {
//				fullBuild(monitor);
//			} else {
//				incrementalBuild(delta, monitor);
//			}
		}
		return null;
	}

	void checkXML(IResource resource) {
		System.out.println("EstructureClassBuilder checkXML resource:"+resource);
		if (resource instanceof IFile && resource.getName().endsWith(".xml")) {
			System.out.println("EstructureClassBuilder checkXML resource.getName():"+resource.getName());
			IFile file = (IFile) resource;
			System.out.println("EstructureClassBuilder checkXML file:"+file.getLocationURI());
			deleteMarkers(file);
			XMLErrorHandler reporter = new XMLErrorHandler(file);
			try {
				getParser().parse(file.getContents(), reporter);
			} catch (Exception e1) {
			}
		}
	}

	private void deleteMarkers(IFile file) {
		try {
			System.out.println("EstructureClassBuilder deleteMarkers file:"+file);
			file.deleteMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO);
		} catch (CoreException ce) {
		}
	}

	protected void fullBuild(final IProgressMonitor monitor)
			throws CoreException {
//		try {
//			System.out.println("EstructureClassBuilder fullBuild monitor:"+monitor);
////			getProject().accept(new SampleResourceVisitor());
//		} catch (CoreException e) {
//			
//		}
	}

	private SAXParser getParser() throws ParserConfigurationException,
			SAXException {
		if (parserFactory == null) {
			parserFactory = SAXParserFactory.newInstance();
		}
		return parserFactory.newSAXParser();
	}

	protected void incrementalBuild(IResourceDelta delta,
			IProgressMonitor monitor) throws CoreException {
		// the visitor does the work.
		System.out.println("EstructureNatureClassBuilder incrementalBuild ");
		System.out.println("EstructureNatureClassBuilder incrementalBuild delta:"+delta);
		System.out.println("EstructureNatureClassBuilder incrementalBuild monitor:"+monitor);
//		delta.accept(new SampleDeltaVisitor());
	}
}
